/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controlers;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import model.Checks;
import model.Customer;

/**
 *
 * @author Tasos
 * O Check Controler parexei yphresies pou aforoun allages pou ginontai stis epitages
 * Checks
 */
public class ChecksControler {

    private static EntityManager em4;    
    //o Customer
    private Integer Customer;
    public ChecksControler() {
     //dhmiourgia Entity Manager
     EntityManagerFactory emf = Persistence.createEntityManagerFactory("SuperMarketPU");
     em4 = emf.createEntityManager();              
     }

//proshtetei mia epitagh sto Checks me to anagnwistiko toy pelath
public void addCheck(Customer cust)
{
//dhmiouriga ths epitaghs
Checks tempCheck = new Checks(0,Calendar.getInstance().getTime().toString(),true);
tempCheck.setCustomerId(cust);
//eisagwsh sthn DB
em4.getTransaction().begin();
em4.persist(tempCheck);
em4.getTransaction().commit();
}

//thetei mia epitagh ws xrhsimopoihmenh
public void disableDefinedCheck(int checkID)
{
        // Dimiouriga toy Query
        TypedQuery<Checks> findCheckquery = em4.createNamedQuery("Checks.findByCheckid", Checks.class);
        findCheckquery.setParameter("checkid", checkID);
        //apothikeysh results
        List<Checks> checkResults = findCheckquery.getResultList();      
        //afou kathe epitagh exei monadiko ID tha exoume mia monadikh eggrafh
        Checks c = checkResults.get(0);
        //enhmerwsh ths eggrafhs sto pedio Valid san false
        em4.getTransaction().begin();
        c.setValid(false);
        em4.merge(c);
        em4.getTransaction().commit();
        } 

//epistrefei mia lista me ths epitages enos sygekrimenou pelath
public List<Checks> getCustomerChecks(String customerID)
{
        //dhmiourgia Query kai epistrofh results
        TypedQuery<Checks> findCheckquery = em4.createNamedQuery("Checks.findAll", Checks.class);
        List<Checks> checkResults = findCheckquery.getResultList();
        return checkResults;
}


}



